package com.wishwall.persistence.managers;

import java.sql.Timestamp;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import com.wishwall.beans.DeviceBean;
import com.wishwall.exceptions.WishWallException;
import com.wishwall.persistence.PersistenceContext;
import com.wishwall.persistence.TransactionalAction;

/**
 * 
 * @author yavor.gologanov
 * 
 */
public class DeviceManager extends AbstractBeanManager {

	/**
	 * 
	 * @param persistenceContext
	 */
	public DeviceManager(PersistenceContext persistenceContext) {
		super(persistenceContext);
	}

	/**
	 * 
	 * @return
	 */
	public void register(DeviceBean device, Integer platformCode, Integer brandCode) {


		device.setDeviceUid(PersistenceContext.createUUID());
		device.setBrandId(1);
		device.setPlatformId(2);
		device.setDateRegistred(new Timestamp(System.currentTimeMillis()));
		storeEntity(device);
		
		device.setDeviceUid(PersistenceContext.createUUID());
		device.setDateRegistred(new Timestamp(System.currentTimeMillis()));
		storeEntity(device);
	}

	/**
	 * 
	 * @param device
	 * @param deviceId
	 * @throws WishWallException
	 */
	public void modifyDevice(DeviceBean device, int deviceId) throws WishWallException {
		DeviceBean existingDevice = getDevice(deviceId);
		updateDevice(device, existingDevice);

	}

	/**
	 * 
	 * @param device
	 * @param deviceUid
	 * @throws WishWallException
	 */
	public void modifyDevice(DeviceBean device, String deviceUid) throws WishWallException {

		DeviceBean existingDevice = getDeviceByUID(deviceUid);

		if (existingDevice == null) {
			throw new EntityNotFoundException("The device with uid " + deviceUid
					+ " does not exist!");
		}

		updateDevice(device, existingDevice);
	}

	/**
	 * 
	 * @param deviceId
	 * @return
	 * @throws Exception
	 */
	public DeviceBean getDevice(int deviceId) throws WishWallException {

		return findEntity(DeviceBean.class, deviceId);
	}

	/**
	 * 
	 * @param deviceUid
	 * @return
	 * @throws WishWallException
	 */
	public DeviceBean getDeviceByUID(String deviceUid) throws WishWallException {

		try {
			Query query = getEntityManager().createQuery("select d from DeviceBean d where d.deviceUid = :uid");
			query.setParameter("uid", deviceUid);
			return (DeviceBean) query.getSingleResult();
		} catch (Exception e) {
			throw new WishWallException("Invalid UID!");
		}

	}

	/**
	 * 
	 * @param deviceUid
	 * @return
	 * @throws WishWallException
	 */
	public List<DeviceBean> getRegistredDevices(Integer issueId) throws WishWallException {

		Query query = getEntityManager().createQuery(
				"select d from DeviceBean d where d.brandId = :brandId");
		return query.getResultList();
	}

	/**
	 * 
	 * @param device
	 * @param existingDevice
	 */
	private void updateDevice(DeviceBean device, DeviceBean existingDevice) {

		if (device.getDeviceName() != null) {
			existingDevice.setDeviceName(device.getDeviceName());
		}

		if (device.getOsVersion() != null) {
			existingDevice.setOsVersion(device.getOsVersion());
		}

		if (device.getCountry() != null) {
			existingDevice.setCountry(device.getCountry());
		}

		if (device.getTimeZone() != null) {
			existingDevice.setTimeZone(device.getTimeZone());
		}

		existingDevice.setLastModifiedDate(new Timestamp(System.currentTimeMillis()));

		device.setId(existingDevice.getId());
	}

	/**
	 * 
	 * @param brandId
	 * @return
	 */
	public List<DeviceBean> listDevices(int brandId) {

		String queryString = "select s from DeviceBean s where s.brandId = :id";
		List<DeviceBean> devices = findAllEntities(queryString, -1, "id", brandId);
		return devices;
	}

}